var express = require('express');
var path = require('path');

var app = express();
var artTemplate = require('express-art-template');


var fs = require("fs");

if (!fs.existsSync('./mes.json')) {
    fs.writeFileSync('./mes.json',[])
  }


app.engine('html', artTemplate);

app.set('views', path.join(__dirname, 'views'));

var meslist =JSON.parse(fs.readFileSync('./mes.json','utf-8')||"[]");
app.get('/mess', function(req, res){
var mes=req.query.content
var now=new Date();
var time=now.getFullYear()+"年"+now.getMonth()+1+"月"+now.getDate()+"日"
var timetwo=now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()
var host="127.0.0.1";
meslist.unshift({
    message:mes,
    time1:time,
    time2:timetwo,
    hosts:host
})

var jsonStr = JSON.stringify(meslist);
    fs.writeFileSync('./mes.json', jsonStr);

  
    res.redirect("/");

 
});

 
app.get('/', function(req, res){
 if (!fs.readFileSync("./mes.json","utf-8")=='') {
     var lam=JSON.parse(fs.readFileSync("./mes.json","utf-8"));
 }
   
    res.render('index.html',{lam});
});


app.listen(5555, function() {
    console.log("服务器启动...");
  });